package net.edu_soft.ec.asset.controller;


import net.edu_soft.ec.asset.constant.EduConstant;
import net.edu_soft.ec.asset.pojo.Feedback;
import net.edu_soft.ec.asset.pojo.UserInfo;
import net.edu_soft.ec.asset.service.FeedbackService;
import net.edu_soft.ec.common.util.EduCloudsUtils;
import net.edu_soft.ec.common.util.PuingUtil;
import net.edu_soft.ec.common.util.ResultVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.management.openmbean.OpenDataException;
import javax.servlet.http.HttpSession;

/**
 * @author: PUING
 * @Date: 2020/6/3 21:42
 * @Description: 信息反馈控制器
 */
@RestController
@RequestMapping("/feedback")
public class FeedbackController {

    @Autowired
    private FeedbackService feedbackService;

    @RequestMapping(value = "/find")
    @RequiresPermissions("feedback:find")
    public ResultVo find(Integer feedbackId, HttpSession session) {
        //得到用户id
        UserInfo userInfo = EduCloudsUtils.getUserInfo(session);
        Integer userInfoId = null;
        //普通用户查询自己的，管理员查询所有
        if(userInfo.getRoleId()==1){
            userInfoId =  userInfo.getUserInfoId();
        }
        Feedback feedback = feedbackService.find(feedbackId, userInfoId);
        String m = feedback == null ? "查询失败" : "成功";
        return ResultVo.succeed(m, feedback);
    }

    @RequestMapping(value = "/findList")
    @RequiresPermissions("feedback:findList")
    public ResultVo findList(Integer pageNum, Integer pageSize, HttpSession session) {
        UserInfo userInfo = EduCloudsUtils.getUserInfo(session);
        Integer userInfoId = null;
        //普通用户查询自己的，管理员查询所有
        if(userInfo.getRoleId()==1){
            userInfoId =  userInfo.getUserInfoId();
        }
        return ResultVo.succeed("", feedbackService.findList(userInfoId, pageNum, pageSize));
    }

    @RequestMapping(value = "/save")
    @RequiresPermissions("feedback:save")
    public ResultVo save(String content, HttpSession session) throws OpenDataException {
        Assert.hasLength(content,"添加反馈：反馈信息为空");
        Feedback feedback = new Feedback();
        feedback.setUserName(EduCloudsUtils.getUserInfo(session).getUsername());
        feedback.setUserInfoId(EduCloudsUtils.getUserInfo(session).getUserInfoId());
        feedback.setContent(content);
        return ResultVo.succeed(feedbackService.save(feedback) >= 1 ? "反馈成功" : "反馈失败", null);
    }

    @RequestMapping(value = "/update")
    @RequiresPermissions("feedback:update")
    public ResultVo update(Feedback feedback, HttpSession session) throws OpenDataException {
        if (feedback != null) {
            PuingUtil.notNullByZero(feedback.getUserInfoId(), "修改反馈：用户id非法");
            if (EduCloudsUtils.getUserInfo(session).getUserInfoId() != feedback.getUserInfoId()) {
                throw new OpenDataException("反馈信息与当前用户不匹配");
            }
        } else {
            throw new OpenDataException("反馈信息不全");
        }
        //判断是不是管理员,是就审核
        if(EduCloudsUtils.getUserInfo(session).getRoleId()==2){
            feedback.setStatus(EduConstant.STATUS_ZREO);
        }else {
            feedback.setStatus(EduConstant.STATUS_ONE);
        }
        return ResultVo.succeed(feedbackService.update(feedback) >= 1 ? "反馈修改成功" : "反馈修改失败", null);
    }

    @RequestMapping(value = "/del")
    @RequiresPermissions("feedback:del")
    public ResultVo del(String ids, Integer isDel) {
        return ResultVo.succeed(feedbackService.del(ids, isDel) >= 1 ? "反馈删除成功" : "反馈删除失败", null);
    }

}
